package config;

import java.io.*;
import java.lang.reflect.Field;

public class ServerConfig {
    public static final String LOOP_BACK = "127.0.0.1";
    public static final Integer DEFAULT_PORT = 30000;
    public static final String DEFAULT_AOF_FILE = "jodis.aof";
    public static final String DEFAULT_RDB_FILE = "dump.rdb";

    public String bind;
    public Integer port;
    public Boolean appendOnly;
    public String appendFileName;
    public Integer maxClients;
    public Boolean requirePass;
    public Integer databases;
    public String rdbFileName;
    public String masterAuth;
    public Integer slaveAnnouncePort;
    public String slaveAnnounceIp;
    public Integer replTimeout;

    public String[] peers;
    public String self;

    public Integer slowLogLogSlowerThan;
    public Integer slowLogMaxLen;

    public Integer ServerMode;
    public String configFileName;

    public static ServerConfig getDefaultConfig() {
        ServerConfig ret = new ServerConfig();
        ret.bind = LOOP_BACK;
        ret.port = DEFAULT_PORT;
        ret.appendOnly = false;
        ret.appendFileName = DEFAULT_AOF_FILE;
        ret.rdbFileName = DEFAULT_RDB_FILE;
        return ret;
    }

    public static ServerConfig setupConfig(String configFileName) {
        File file = new File(configFileName);
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)))) {
            String line;
            Class<ServerConfig> cls = ServerConfig.class;
            ServerConfig ret = new ServerConfig();
            Field[] fields = cls.getFields();
            while ((line = reader.readLine()) != null) {
                String[] ls = line.strip().split("\\s");
                for (Field f : fields) {
                    if (f.getName().equalsIgnoreCase(ls[0].strip())) {
                        Class fieldCls = f.getType();
                        if (fieldCls.equals(Boolean.class)) {
                            f.setBoolean(ret, ls[1].strip().equalsIgnoreCase("yes"));
                        } else if (fieldCls.equals(String.class)) {
                            f.set(ret, ls[1].strip());
                        } else if (fieldCls.equals(Integer.class)) {
                            f.setInt(ret, Integer.parseInt(ls[1].strip()));
                        }
                    }
                }
            }
            return ret;
        } catch (IOException | IllegalAccessException e) {
            e.printStackTrace();
        }

        return null;
    }

    public static void main(String[] args) {
        for (String s : args) {
            System.out.println(s);
        }
        Class<ServerConfig> cls = ServerConfig.class;
        Field[] fields = cls.getFields();
        File file = new File("jodis_master.cfg");
        System.out.println(file.getAbsolutePath());
        for (Field f : fields) {
//            System.out.println(f.getName());
            if (f.getName().equalsIgnoreCase("appendOnly")) {
                Class fieldCls = f.getType();
                if (fieldCls.equals(Boolean.class)) {
                    System.out.println(fieldCls.getName());
                }
            }
        }
    }

}


